
#include <libavcodec/avcodec.h>

#include <stdio.h>

int main(int argc, char *argv[])
{
    FILE *fp1 = fopen(argv[1], "rb");
    FILE *fp2 = fopen(argv[2], "rb");
    
    int ff_ret;
    
    AVCodec *h264 = avcodec_find_decoder(AV_CODEC_ID_H264);
    AVCodecContext *contex = avcodec_alloc_context3(h264);
    
    ff_ret = avcodec_open2(contex, h264, NULL);
    printf("avcodec_open2(), ret: %d\n", ff_ret);

    while (!feof(fp1))
    {
        AVPacket *pkt = av_packet_alloc();
        unsigned len;
        fread(&len, sizeof(len), 1, fp1);
        av_new_packet(pkt, len);
        fread(pkt->data, len, 1, fp1);
        pkt->size = len;
        ff_ret = avcodec_send_packet(contex, pkt);
        printf("1, avcodec_send_packet(), ret: %d\n", ff_ret);
        av_packet_free(&pkt);
        
        do
        {
            AVFrame *frame = av_frame_alloc();
            ff_ret = avcodec_receive_frame(contex, frame);
            printf("1, avcodec_receive_frame(), ret: %d, format: %d, size: %dx%d, strides: [%d %d %d]\n", 
                ff_ret, frame->format, frame->width, frame->height, frame->linesize[0], frame->linesize[1], frame->linesize[2]);
            av_frame_free(&frame);
        } while(ff_ret == 0);
    }
    
    while (!feof(fp2))
    {
        AVPacket *pkt = av_packet_alloc();
        unsigned len;
        fread(&len, sizeof(len), 1, fp2);
        av_new_packet(pkt, len);
        fread(pkt->data, len, 1, fp2);
        pkt->size = len;
        ff_ret = avcodec_send_packet(contex, pkt);
        printf("2, avcodec_send_packet(), ret: %d\n", ff_ret);
        av_packet_free(&pkt);
        
        do
        {
            AVFrame *frame = av_frame_alloc();
            ff_ret = avcodec_receive_frame(contex, frame);
            printf("2, avcodec_receive_frame(), ret: %d, format: %d, size: %dx%d, strides: [%d %d %d]\n", 
                ff_ret, frame->format, frame->width, frame->height, frame->linesize[0], frame->linesize[1], frame->linesize[2]);
            av_frame_free(&frame);
        } while(ff_ret == 0);
    }

    avcodec_free_context(&contex);

    fclose(fp1);
    fclose(fp2);

    return 0;
}
